package com.phone.dao.impl;


import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.phone.dao.UserDao;
import com.phone.entity.Admin;
import com.phone.entity.Store;
import com.phone.entity.User;
@Repository
public class UserDaoimpl implements UserDao {
	@Autowired
    private SessionFactory sessionFactory; 
	@Override
	public void saveuser(User user) {
		this.sessionFactory.getCurrentSession().save(user);
	}
	@Override
	public void savestore(Store store) {
		this.sessionFactory.getCurrentSession().save(store);
		
	}
	@Override
	public User seleu(String username) {
		User user=null;
		String hql="from User where username=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			user = (User) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public Admin selea(String username) {
		Admin admin=null;
		String hql="from Admin where username=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			admin = (Admin) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}
	@Override
	public Store seles(String username) {
		Store store=null;
		String hql="from Store where username=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			store = (Store) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return store;
	}
	@Override
	public User seleuser(String username, String password) {
		User user=null;
		String hql = "from User where username=? and password=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			user= (User) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public Admin seleadmin(String username, String password) {
		Admin admin= null;
		String hql = "from Admin where username=? and password=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			admin= (Admin) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}
	@Override
	public Store selestore(String username, String password) {
		Store store=null;
		String hql="from Store where username=? and password=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			store=(Store) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return store;
	}
	@Override
	public boolean upuser(User user) {
		this.sessionFactory.getCurrentSession().update(user);
		return true;
	}
	

}
